package com.jp.knowledge.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.HeadItemAdapter;
import com.jp.knowledge.adapter.OnItemListerner;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.callback.ItemDragHelperCallback;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.db.DBHelper;
import com.jp.knowledge.dialog.ComPopuWindow;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.HeadLine;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.List;

import rx.Subscriber;

public class EditHeadLineAcitvity extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener {

    private Context mContext;
    //拖动gridView
    private RecyclerView sortView;
    private TextView sureBtn;//确定按钮
    private HeadItemAdapter adapter;

    private ComPopuWindow.DragItemHelper dragItemHelper;
    private List<HeadLine> list;

    private int lastPostion;
    private String infoId;//infoId
    private String navType;//类型id

    private LocalBroadcastManager localManager;
    private OnItemListerner onItemListerner  =new OnItemListerner() {
        @Override
        public void onItemClick(View view, int position) {
            navType =  list.get(position).getTypeId();
             list.get(lastPostion).setCheck(false);
                 list.get(position).setCheck(true);
                 adapter.notifyDataSetChanged();
            lastPostion =position;
        }
    };

    @Override
    protected int getResourceId() {
        return R.layout.activity_edit_head_line_acitvity;
    }

    @Override
    protected void init() {
        localManager = LocalBroadcastManager.getInstance(mContext);
        infoId = getIntent().getStringExtra("infoId");
        this.mContext =this;
        setHeadTitle();
        sureBtn = (TextView) findViewById(R.id.sure);
        sortView = (RecyclerView)findViewById(R.id.activity_sort_head);
        sureBtn.setOnClickListener(this);
        DBHelper.getInstance().findAll(HeadLine.class, new Subscriber<List<HeadLine>>() {
            @Override
            public void onCompleted() {
//                ToasUtil.toast(context, "onCompleted");
            }
            @Override
            public void onError(Throwable e) {
                ToasUtil.toast(mContext, "获取数据失败");
            }

            @Override
            public void onNext(List<HeadLine> headLines) {
                list = headLines;
                if (headLines != null && headLines.size() > 0) {
                    bindViewData();
                }
            }
        });

    }

    /**
     * 修改标题
     */
    private void setHeadTitle() {
        topName.setText("分类校正");
        topDesc.setVisibility(View.GONE);
        topName.setCompoundDrawables(null,null,null,null);
        rightIcon.setVisibility(View.GONE);
        leftIcon.setVisibility(View.VISIBLE);
        leftIcon.setImageResource(R.mipmap.guanzhufanhui);
        leftIcon.setOnClickListener(this);
    }


    /**
     * 校正接口调用
     */
    private void correcting(){
        if(infoId==null||navType==null){
            ToasUtil.toast(mContext,"资讯id或者分类id为空");
            return;
        }


        JsonObject param = new JsonObject();
        param.addProperty("id",infoId);
        param.addProperty("navType",navType);

        HttpHelper.getInstance(mContext).correctingNavType(param,1,this);
    }

    /**
     * 绑定数据
     */
    private void bindViewData() {
        adapter = new HeadItemAdapter(mContext,list);
        adapter.setOpenTopic(true);
        adapter.setItemListerner(onItemListerner);
//        adapter.setDragItemHelper(dragItemHelper);
        GridLayoutManager layoutMannager = new GridLayoutManager(mContext, 4, LinearLayoutManager.VERTICAL, false);

        sortView.setLayoutManager(layoutMannager);
        sortView.setItemAnimator(new DefaultItemAnimator());
        sortView.setAdapter(adapter);
//        initItemDrag();
    }


    public List<HeadLine> getHeadLine(){
        return adapter.getData();
    }
    /**
     * 初始化拖拽
     */
    private void initItemDrag() {
        ItemDragHelperCallback itemDragHelperCallback = new ItemDragHelperCallback(adapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragHelperCallback);
        itemTouchHelper.attachToRecyclerView(sortView);
        adapter.setItemTouchHelper(itemTouchHelper);
        adapter.setItemDragHelperCallback(itemDragHelperCallback);
    }

    public void updateData(){//刷新数据
        adapter.notifyDataSetChanged();
    }


    public List<HeadLine> getList() {
        return list;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.icon_left:
                this.finish();
                break;
            case  R.id.sure:
                correcting();
                break;
            default:
                break;
        }
    }

    @Override
    public void onStart(int requestType) {
        loading.show();
    }

    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }

    @Override
    public void onError(int requestType) {
        loading.cancel();
        ToasUtil.toast(mContext,"校正失败!");
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode()==0){
            ToasUtil.toast(mContext,"校正成功!");

            //发送广播通知 前业刷新数据
            Intent intent = new Intent(LocalBroadCast.HEAD_NAV_CORRECTING);
            localManager.sendBroadcast(intent);


            this.finish();
        }else {
            ToasUtil.toast(mContext,"校正失败!");
        }
    }

    public interface  DragItemHelper{
        void drag(int fromPositon,int toPosition);
    }
}
